﻿Ext.define('app.system.laboratory.Edit',
{
    extend: 'Ext.window.Window',
    autoScroll: true,
    modal: true,
    //maximized: true,
    width: 600,
    height: 400,
    layout: 'fit',
    laboratoryId: 0,
    rdCenterId: 0,
    bodyStyle: 'padding:30px',
    initComponent: function () {
        var me = this;
        me.items = [me.getForm()];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSubmit },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    listeners: {
        afterrender: function (win) {
            if (win.laboratoryId > 0) {
                App.AjaxGet('/api/services/app/laboratory/Get?laboratoryId=' + win.laboratoryId, function (data) {
                    console.log(data.result.principal);
                    var cbo_principal = win.form.query("combo[name=principalId]")[0];
                    cbo_principal.store.load({
                        callback: function () {
                            cbo_principal.setValue(data.result.principalId);
                            cbo_principal.setRawValue(data.result.principal);
                        }
                    });
                    win.form.getForm().setValues(data.result);
                });
            }
        }
    },
    getForm: function () {
        var me = this;
        if (!this.form) {
            this.form = new Ext.form.Panel({
                defaultType: 'textfield',
                border: false,
                defaults: { anchor: '100%' },
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        hidden: true,
                        name: 'id',
                        value: 0
                    },
                    {
                        fieldLabel: '名称',
                        allowBlank: false,
                        name: 'name'
                    },
                    {
                        xtype: 'combo',
                        //id: 'cbo_principal',
                        name: 'principalId',
                        fieldLabel: '室主任',
                        store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/user/GetUsers', autoLoad: false }),
                        queryParam: 'name',
                        typeAhead: false,
                        emptyText: '输入姓名查询',
                        minChars: 1,
                        queryMode: 'remote',
                        triggerAction: 'query',
                        displayField: 'name',
                        valueField: 'id'
                    },
                    {
                        xtype: 'hidden',
                        name: 'rdCenterId',
                        value: me.rdCenterId
                        //fieldLabel: '研发中心',
                        //editable: false,
                        //store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/rdCenter/GetAll' }),
                        //queryMode: 'local',
                        //displayField: 'name',
                        //valueField: 'id',
                    }
                ]
            });
        }
        return this.form;
    },
    onSubmit: function () {
        var me = this;
        var name = me.form.query("combo[name=principalId]")[0].getRawValue();
        var json = me.getForm().getForm().getValues();
        json.Principal = name;
        App.AjaxPost('/api/services/app/laboratory/CreateOrEdit', json, function () {
            me.fireEvent("savesuccess");
        });
    }
});